package com.example.mywiseelves.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mywiseelves.R;
import com.example.mywiseelves.dao.ElvesDao;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WisdomFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WisdomFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    //智慧水晶，文字
    TextView openWisdomTextView;
    TextView closeWisdomTextView;
    TextView setWisdomTextView;

    //精灵dao对象
    ElvesDao elvesDao;
    int wisdom;
    //获取布局
    View view;
    public WisdomFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment WisdomFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static WisdomFragment newInstance(String param1, String param2) {
        WisdomFragment fragment = new WisdomFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        //精灵dao对象
        elvesDao=new ElvesDao(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_wisdom, container, false);
        findViews();
        initViews();
        initListener();
        return view;
    }

    /**
     * 实例化控件
     * */
    private void findViews() {
        closeWisdomTextView=view.findViewById(R.id.closeWisdom_tv);
        openWisdomTextView=view.findViewById(R.id.openWisdom_tv);
        setWisdomTextView=view.findViewById(R.id.setWisdom_tv);
    }

    /**
     * 初始化控件
     */
    private void initViews() {
        wisdom=elvesDao.allElvesQuery().get(0).getElves_wisdom();
        if (wisdom==0){
            openWisdomTextView.setVisibility(View.VISIBLE);
            closeWisdomTextView.setVisibility(View.GONE);
        }else if(wisdom==1){
            openWisdomTextView.setVisibility(View.GONE);
            closeWisdomTextView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 刷新控件
     */
    private void refreshView() {
        wisdom=elvesDao.allElvesQuery().get(0).getElves_wisdom();
        if (wisdom==0){
            openWisdomTextView.setVisibility(View.VISIBLE);
            closeWisdomTextView.setVisibility(View.GONE);
        }else if(wisdom==1){
            openWisdomTextView.setVisibility(View.GONE);
            closeWisdomTextView.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 初始化监听
     */
    public void initListener() {
        openWisdomTextView.setOnClickListener(this);
        closeWisdomTextView.setOnClickListener(this);
        setWisdomTextView.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.openWisdom_tv){
            initPermission();
            elvesDao.elvesWisdomUpdate(1);
            refreshView();
            Toast.makeText(getContext(),"精灵智慧开启成功", Toast.LENGTH_LONG).show();
        }else if (v.getId()==R.id.setWisdom_tv){
            if (wisdom==1){
                Intent intent=new Intent("setWisdomActivity");
                startActivity(intent);
            }else {
                Toast.makeText(getContext(),"请先开启精灵智慧", Toast.LENGTH_LONG).show();
            }

        }
        else if (v.getId()==R.id.closeWisdom_tv){
            elvesDao.elvesWisdomUpdate(0);
            refreshView();
            Toast.makeText(getContext(),"精灵智慧已关闭", Toast.LENGTH_LONG).show();
        }
    }
    /**
     * 初始化权限
     * */
    private void initPermission() {
        String permissions[] = {Manifest.permission.RECORD_AUDIO,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };

        ArrayList<String> toApplyList = new ArrayList<String>();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(getActivity(), perm)) {
                toApplyList.add(perm);
            }
        }
        String tmpList[] = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(getActivity(), toApplyList.toArray(tmpList), 123);
        }
    }
}